package cn.melon.view.admin.dorm;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import cn.melon.mod.dorm.DormInfo;
import cn.melon.view.admin.dorm.ctr.DormInfoDlgCtr;

public class DormInfoDlg extends JDialog {
    /**
     * 
     */
//    private ActionListener listener = new LIbraryBookInfoCtr(this);
    private ActionListener listener;
    
    private static final long serialVersionUID = 1L;
    private JTextField dormNum = new JTextField("",20);
    private JTextField studentNum = new JTextField("",20);
    private JTextField newHonor = new JTextField("",20);
    private JTextField newScore = new JTextField("",20);
    private JTextField waterAndEletricityCharge = new JTextField("",20);

    
    private JButton saveButton = new JButton("保存");
    private JButton cancelButton = new JButton("取消");
    String[] textStr = {"宿舍号：","学生学号：","宿舍荣誉：","宿舍分数：","水电费："};
    
    JTextField[] jTextFields = {dormNum,studentNum,newHonor,newScore,waterAndEletricityCharge};
    
    public DormInfoDlg(DormInfo dormInfo, IUpdateDormTable updateTable) {
        listener= new DormInfoDlgCtr(this, updateTable);
        initialize();
        dormNum.setText(dormInfo.getDormNum());
        studentNum.setText(dormInfo.getStudentNum());
        newHonor.setText(dormInfo.getNewHonor());
        newScore.setText(dormInfo.getNewScore());
        waterAndEletricityCharge.setText(dormInfo.getWaterAndEletricityCharge());
    }
    private void initialize(){
        this.setTitle("宿舍信息");
        this.setSize(400, 300);
       
        JPanel panel = new JPanel();
        
        GridBagLayout layout = new GridBagLayout();
        panel.setLayout(layout);        

        for(int i=0;i<textStr.length;i++){
            GridBagConstraints constraints = new GridBagConstraints();
            constraints.anchor = GridBagConstraints.EAST;
            constraints.fill = GridBagConstraints.NONE;
            constraints.gridx = 0;
            constraints.gridy = i;
            constraints.weightx = 100;
            constraints.weighty = 100;
            panel.add(new JLabel(textStr[i]),constraints);
            constraints.anchor = GridBagConstraints.CENTER;
            constraints.gridx = 1;
            panel.add(jTextFields[i],constraints);
            constraints.anchor = GridBagConstraints.EAST;
            constraints.gridx = 2;
            panel.add(Box.createRigidArea(new Dimension(2, 15)));
        }
        
        JPanel tPanel = new JPanel();
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.anchor = GridBagConstraints.CENTER;
        constraints.fill = GridBagConstraints.NONE;
        constraints.gridx = 1;
        constraints.gridy = textStr.length;
        constraints.weightx = 100;
        constraints.weighty = 100;
        tPanel.add(saveButton);     //按钮
        tPanel.add(Box.createRigidArea(new Dimension(10, 15)));
        tPanel.add(cancelButton);
        panel.add(tPanel,constraints);      //按钮
        
        this.add(panel);
        this.setResizable(false);

        saveButton.addActionListener(listener);
        cancelButton.addActionListener(listener);
    }

    public DormInfo getDormInfo(){
        DormInfo dormInfo = new DormInfo();
        dormInfo.setDormNum(dormNum.getText());
        dormInfo.setStudentNum(studentNum.getText());
        dormInfo.setNewHonor(newHonor.getText());
        dormInfo.setNewScore(newScore.getText());
        dormInfo.setWaterAndEletricityCharge(waterAndEletricityCharge.getText());
        return dormInfo;
    }
}
